//
//  WLLTabBar.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// 声明协议  需要继承NSObjectProtocol
protocol WLLTabBarDelegate : NSObjectProtocol {
    //声明方法
    func tabBarButtonClick()
    
}
class WLLTabBar: UITabBar {
    
    weak var wllDelegat :WLLTabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
         setupUI()
    }
    //纯代码
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     给子控件设置布局/ frame
     */
    override func layoutSubviews() {
        //好习惯,
        super.layoutSubviews()
        
        let chirldW = KSCREENW*0.2
        var index:CGFloat = 0
        
        for value in self.subviews{
            
            if value .isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame.size.width = chirldW
                value.frame.origin.x = chirldW*index
                
            index++
                if index == 2 {
                    index++
                }
            }
        }
        //TODO:修改
        composeButton.center.x = KSCREENW*0.5
        composeButton.center.y = self.frame.height*0.5
        
    }
    
    /**
     设置视图
     */
    func setupUI ()  {
        //添加撰写按钮
        addSubview(composeButton)
    }
    
    /**
     监听按钮点击
     */
    func buttonClick()  {
//         print("加号按钮被点击了")
        wllDelegat?.tabBarButtonClick()
    }
    
     //MARK: - 懒加载控件
    /**
     *  撰写按钮
     */
    private lazy var composeButton:UIButton = {
        let button = UIButton()
        button.addTarget(self , action: #selector(WLLTabBar.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)

        button.setBackgroundImage(UIImage(named:"tabbar_compose_button" ), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted" ), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //自 shiying
        button.sizeToFit()
        return button
    }()
    
    
}
